<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;

class RecommendController extends Controller
{

	
	// 竞彩推荐列表
	public function recomList()
	{
		$time = date( 'Y-m-d H:i:s',time() );
		// 列表 数据
		$recommend_all = DB::table('new_recommend as r')
				-> select( 'r.*','u.name as username',DB::raw("replace(u.pic_url,'/images','".config('app.imgPath')."/images' ) as pic_url"),DB::raw("replace(r.home_pic,'/images','".config('app.imgPath')."/images' ) as home_pic"),DB::raw("replace(r.away_pic,'/images','".config('app.imgPath')."/images' ) as away_pic"),'u.id as uid','r.final_home','r.final_away',DB::raw("CASE WHEN r.match_date > DATE_FORMAT( NOW(), '%Y-%m-%d %H:%i:%s') THEN '1' ELSE '0' END AS  isstart  ") )
				-> leftJoin('user as u','u.id','=','r.user_id')
				// 3审核通过 4推荐 5头条
				-> whereIn( 'r.status',array(3,4,5) )
				// 未开赛
				-> orderBy('isstart','desc')
				-> orderBy('r.match_date','desc')
				-> paginate(7);
		$recommends = array();
		foreach( $recommend_all as $key => $recom_info )
		{
			$re = array();
			$re['id']= $recom_info->id;
			$re['username']= $recom_info->username;
			// 获取用户id
			$re['uid']= $recom_info->uid;
			// 判断胜率
			// 1.总发布
			$total = DB::table('new_recommend')
				-> whereIn('status',array(3,4,5))
				-> where('user_id',$recom_info->uid)
				-> count();
			// 2.中的
			$zhong  = DB::table('new_recommend')
				// 中
				-> where('judge',2)
				-> where('user_id',$recom_info->uid)
				-> count();
			if( $total<1 )
			{
				$shenglv = '0';
			}else
			{
				// 2位小数
				$shenglv = ( round($zhong/$total,2)*100 )."%";
			}
			
			$re['total']= $total;
			$re['zhong']= $zhong;
			$re['shenglv']= $shenglv;
			$re['create_at']= $recom_info->create_at;
			$re['pic_url']= $recom_info->pic_url;
			$re['home']= $recom_info->home;
			$re['home_pic']= $recom_info->home_pic;
			$re['away']= $recom_info->away;
			$re['away_pic']= $recom_info->away_pic;
			$re['match_date']= $recom_info->match_date;
			if( strtotime($recom_info->match_date)<time() )
			{
				// 已结束
				$re['isstart']=1;
			}else
			{
				// 未开赛
				$re['isstart']=0;
			}
			$re['home_index']= $recom_info->home_index;
			$re['p_index']= $recom_info->p_index;
			$re['away_index']= $recom_info->away_index;
			$re['final_home']= $recom_info->final_home;
			$re['final_away']= $recom_info->final_away;
			$pattern = "/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i";
			preg_match_all($pattern,$recom_info->content,$arr);
			foreach($arr[0] as $keyy => $str)
			{
				// 找到 img 标签 跳过
				if( strstr($str,'img') || strstr($str,'<p><br/>') || strstr($str,'span') )
				{
					continue;
				}else
				{
					// 简短描述
					$re['content']= rtrim( ltrim( $str,"<p>" ),"<br/></p>" );
					break;
				}
			}
			if( !isset($re['content']) )
			{
				$re['content']='';
			}
			$recommends[$key] = $re;
		
		}
		
		// 专家推荐
		$recommend_tewnty = DB::table('new_recommend as r')
				-> select( 'r.*','u.name as username',DB::raw("replace(u.pic_url,'/images','".config('app.imgPath')."/images' ) as pic_url"),DB::raw("replace(r.home_pic,'/images','".config('app.imgPath')."/images' ) as home_pic"),DB::raw("replace(r.away_pic,'/images','".config('app.imgPath')."/images' ) as away_pic"),'u.id as uid',DB::raw( "CASE WHEN r.match_date > DATE_FORMAT( NOW(), '%Y-%m-%d %H:%i:%s') THEN '1' ELSE '0' END AS isstart") )
				-> leftJoin('user as u','u.id','=','r.user_id')
				// 4推荐
				-> where( 'r.status','=',4 )
				// 未开赛
				-> where( 'r.match_date','>',date('Y-m-d H:i:s',time() ) )
				-> take(20)
				-> get();
		$zhuanjia = array();
		foreach( $recommend_tewnty as $key => $recom_info )
		{
			$zhuan = array();
			$zhuan['id']= $recom_info->id;
			$zhuan['uid']= $recom_info->uid;
			$zhuan['username']= $recom_info->username;
			$zhuan['pic_url']= $recom_info->pic_url;
			$zhuan['home']= $recom_info->home;
			$zhuan['away']= $recom_info->away;
			$pattern = "/<p[^>]*>(?:(?!<\/p>)[\s\S])*<\/p>/i";
			preg_match_all($pattern,$recom_info->content,$arr);
			
			foreach($arr[0] as $keyy => $str)
			{
				// 找到 img 标签 跳过
				if( strstr($str,'img') || strstr($str,'<p><br/>') || strstr($str,'span') )
				{
					continue;
				}else
				{
					// 简短描述
					$zhuan['content']= rtrim( ltrim( $str,"<p>" ),"<br/></p>" );
					break;
				}
			}
			// dd($zhuan['content']);
			if( !isset($zhuan['content']) )
			{
				$zhuan['content']='';
			}
			$zhuanjia[$key] = $zhuan;
		
		}
		
		// dd($zhuanjia);
		
		// 胜率排行榜
		$all_user = DB::table('new_recommend')
			-> select('user_id')
			-> groupBy('user_id')
			-> get();
		$rank = array();
		foreach( $all_user as $key => $user_id )
		{
			// 1.总发布
			$rank_total = DB::table('new_recommend')
				-> whereIn('status',array(3,4,5))
				-> where('user_id',$user_id->user_id)
				-> count();
			// 2.中的
			$rank_zhong  = DB::table('new_recommend')
					// 中
					-> where('judge',2)
					-> where('user_id',$user_id->user_id)
					-> count();
			// 用户信息
			$user_info = DB::table('user')
						-> select( 'id','name',DB::raw("replace(pic_url,'/images','".config('app.imgPath')."/images' ) as pic_url") )
						-> where( 'id',$user_id->user_id )
						-> first();
			if( $rank_total<1 )
			{
				$rank[$key]['shenglv'] = '未发表';
			}else
			{
				// 2位小数
				$rank[$key]['shenglv'] = ( round($rank_zhong/$rank_total,2)*100 )."%";
			}
			$rank[$key]['rank_total'] = $rank_total;
			$rank[$key]['rank_zhong'] = $rank_zhong;
			$rank[$key]['id'] = $user_info->id;
			$rank[$key]['name'] = $user_info->name;
			$rank[$key]['pic_url'] = $user_info->pic_url;
		}
		
		// 按大小排序
		arsort($rank);

        // 赛事列表页 配置
        $webConfig = DB::table('webconfig')
            -> select('webName')
            -> where('id',1)
            -> first();


        // 竞彩推荐列表
		return view('recommend.recommendlist',['webConfig'=>$webConfig, 'recommends'=>$recommends,'recommend_all'=>$recommend_all,'recommend_tewnty'=>$zhuanjia,'rank'=>$rank ]);
	
	}
	
	
	
	
	
	// 赛事推荐详情
	public function recomDetail( $reconmend_id=15 )
	{
		
		// 赛事推荐
		$recommend = DB::table('new_recommend as r')
				-> select('r.*',DB::raw("replace(r.home_pic,'/images','".config('app.imgPath')."/images' ) as home_pic "),DB::raw("replace(r.away_pic,'/images','".config('app.imgPath')."/images' ) as away_pic "),'u.name as username',DB::raw("replace(u.pic_url,'/images','".config('app.imgPath')."/images' ) as user_pic "),'u.id as uid','u.name as username' )
				-> leftJoin('user as u','u.id','=','r.user_id','r.final_away','r.final_home')
				-> where( 'r.id','=',$reconmend_id )
				-> first();
		$recommend->content = str_replace("/images",config('app.imgPath')."/images",$recommend->content);
		$check = explode(',',$recommend->checkbox);
				
		// 获取 评论 
		$comments = DB::table('comments as c')
				-> select( 'u.name','c.create_at','c.content','c.id','c.user_id','c.good',DB::raw("replace(u.pic_url,'/images','".config('app.imgPath')."/images' ) as pic_url "  ) )
				-> leftJoin( 'user as u','u.id','=','c.user_id' )
				-> where( 'c.record_id','=',$reconmend_id )
				// 赛事推荐类型
				-> where( 'c.type','=',4 )
				// 评论 时间
				-> orderBy('c.create_at','desc')
				-> paginate(5);
		
		// 评论 总数
		$comments_total = DB::table('comments as c')
					-> where( 'record_id','=',$reconmend_id )	
					// 赛事推荐类型
					-> where( 'c.type','=',4 )					
					-> count();
					
		// 获取 评论 回复 
		$comments_reply = array();
		foreach( $comments as $key => $comment_info )
		{
			// 评论id
			$comment_id = $comment_info->id;
			$replys = DB::table('reply as r')
						-> select('r.*','u.name as username','uu.name as replyname',DB::raw("replace(uu.pic_url,'/images','".config('app.imgPath')."/images' ) as reply_user_pic "  ))
						-> leftJoin('user as u','u.id','=','r.user_id')
						-> leftJoin('user as uu','uu.id','=','r.reply_user_id')
						-> where( 'r.comment_id','=',$comment_id )	
						// 回复 时间
						-> orderBy('r.create_at')
						-> get();
			$reply = array();
			foreach( $replys as $k => $reply_info )
			{
				
				$reply[$k]['id'] = $reply_info->id;
				$reply[$k]['comment_id'] = $reply_info->comment_id;
				$reply[$k]['good'] = $reply_info->good;
				$reply[$k]['user_id'] = $reply_info->user_id;
				$reply[$k]['content'] = $reply_info->content;
				$reply[$k]['reply_user_id'] = $reply_info->reply_user_id;
				$reply[$k]['create_at'] = $reply_info->create_at;
				$reply[$k]['username'] = $reply_info->username;
				$reply[$k]['replyname'] = $reply_info->replyname;
				$reply[$k]['reply_user_pic'] = $reply_info->reply_user_pic;
			}
			$comments_reply[$key] = $reply;
		}
		
		// 推荐
		$tuijian = DB::table('new_recommend as r')
				-> select('r.id','r.home','r.away',DB::raw("replace(r.home_pic,'/images','".config('app.imgPath')."/images' ) as pic_url") )
				// 4推荐 
				-> where( 'r.status','=','4' )
				// 未开赛
				-> where( 'r.match_date','>',date('Y-m-d H:i:s',time() ) )
				-> take(10)
				-> get();

        // 赛事列表页详情 配置
        $webConfig = DB::table('webconfig')
            -> select('webName')
            -> where('id',1)
            -> first();

        return view('recommend.recommenddetail',['webConfig'=>$webConfig,'recommend'=>$recommend,'comments_total'=>$comments_total,'comments'=>$comments,'comments_reply'=>$comments_reply,'tuijian'=>$tuijian,'check'=>$check]);
	}
	
	
}
